package klok;

import javax.swing.*;
import java.awt.*;

public class KlokPanel extends JPanel implements Runnable {
  /**
	 * 
	 */
	private static final long serialVersionUID = 3624856170976201870L;
	public static final int KLOKSTRAAL = 100;
	private int uur;
	private int minuut;
	private int seconde;
	private Thread thread;
	private boolean draait;

  public KlokPanel () {
    uur=0;
    minuut=0;
    seconde=0;
    this.setSize(200, 200);
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.white);
    g.fillOval(0,0,200,200);
    g.setColor(Color.black);
    g.drawOval(0,0,200,200);
    drawSecondeWijzer(g);
    drawMinuutWijzer(g);
    drawUurWijzer(g);
  }
  
  //extract methods voor wijzers.
  private void drawSecondeWijzer(Graphics g){
	  g.drawLine(100, 100,
			  (int)(100 + 98 * Math.sin(seconde / 30.0 * Math.PI)),
              (int)(100 - 98 * Math.cos(seconde / 30.0 * Math.PI)));
  }
  
//extract methods voor wijzers.
  private void drawMinuutWijzer(Graphics g){
	  g.drawLine(100, 100,
              (int)(100 + 95 * Math.sin(minuut / 30.0 * Math.PI)),
              (int)(100 - 95 * Math.cos(minuut / 30.0 * Math.PI)));;
  }
  
  //extract methods voor wijzers
  private void drawUurWijzer(Graphics g){
	  g.drawLine(100, 100,
              (int)(100 + 60 * Math.sin((60 * uur + minuut) / (30.0 * 12) * Math.PI)),
              (int)(100 - 60 * Math.cos((60 * uur + minuut) / (30.0 * 12) * Math.PI)));
  }
  
  public void run () {
    while (draait) {
      try {
        Thread.sleep(1000);
      }
      catch (InterruptedException e) {}
      seconde = (seconde + 1) % 60;
      if (seconde == 0) {
        minuut = (minuut + 1) % 60;
        if (minuut == 0) {
          uur = (uur + 1) % 24;
        }
      }
      repaint();
    }
  }

  public void start() {
    thread = new Thread(this);
    thread.setDaemon(true);
    draait = true;
    thread.start();
  }
  public void stop() {
    draait = false;
  }

}
